package com.br.android.widing;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widing.db.WidingCategoriaDAO;

public class ExibicaoCategoriaActivity extends SherlockActivity {

	private TextView textDescricao;
	private int idCategoria;

	private Intent intentGlobal;
	private WidingCategoriaDAO daoCategoria;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_exibicao_categoria);
		daoCategoria = new WidingCategoriaDAO(this);
		textDescricao = (TextView) findViewById(R.id.tvDescricao);
		intentGlobal = getIntent();
		idCategoria = intentGlobal.getExtras().getInt("id");
		textDescricao.setText(intentGlobal.getExtras().getString("categoria"));

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_exibicao_item, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_editar_item:
			Intent intent = new Intent(this, EditGastoActivity.class);
			intent.putExtra("id", idCategoria);
			intent.putExtra("categoria",
					intentGlobal.getExtras().getString("categoria"));

			startActivity(intent);
			overridePendingTransition(android.R.anim.fade_in,
					android.R.anim.fade_out);
			finish();
			break;
		case R.id.menu_excluir_item:
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle("Widing");
			dialog.setIcon(R.drawable.ic_launcher);
			dialog.setMessage("Tem certeza que deseja excluir esta categoria?");

			dialog.setPositiveButton("Sim", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					daoCategoria.excluirCategoria(idCategoria);
					startActivity(new Intent(ExibicaoCategoriaActivity.this,
							PrincipalActivity.class));
					overridePendingTransition(android.R.anim.fade_in,
							android.R.anim.fade_out);
					mensagem("Categoria exclu\u00edda com sucesso.");
					finish();
					daoCategoria.close();
				}
			});

			dialog.setNegativeButton("Cancelar", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});

			dialog.show();
			break;
		default:
			startActivity(new Intent(this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in,
					android.R.anim.fade_out);
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void mensagem(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			startActivity(new Intent(this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in,
					android.R.anim.fade_out);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
